package shark.oss;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import shark.oss.impl.OssStorageServiceImpl;
import shark.oss.model.ServerDefine;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class OssConfig {

    @Autowired
    private Environment environment;

    @Bean
    public OssStorageService ossStorageService(){
        String prefix = "shark.oss";
        List<ServerDefine> serverDefines = new ArrayList<>();

        Binder binder = Binder.get(environment);
        for(int i=0;;i++){
            String key = prefix + "[" + Integer.valueOf(i) + "]";
            if(environment.containsProperty(key+"name")){
                ServerDefine define = binder.bind(key, Bindable.of(ServerDefine.class)).get();
                serverDefines.add(define);
            }else {
                break;
            }
        }
        return new OssStorageServiceImpl(serverDefines);
    }
}
